package com.dhu.controller;

import com.alibaba.fastjson.JSONObject;
import com.dhu.pojo.User;
import com.dhu.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpRequest;
import org.springframework.util.ResourceUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.commons.CommonsMultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.FileNotFoundException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;
import java.util.UUID;

@CrossOrigin
@RestController
public class UserController {

    @Autowired
    private UserService userService;

    @PostMapping("/login")
    public String login(@RequestBody Map<String, String> map) {
        JSONObject respond = new JSONObject();
        System.out.println(map);
        if (userService.selectByUsername(map.get("username")) != null) {
            User user = userService.selectByUsernameAndPassword(map.get("username"), map.get("password"));
            if (user != null) {
                respond.put("code", 200);
                respond.put("message", "登录成功");
                respond.put("data", user);
            } else {
                respond.put("code", 404);
                respond.put("message", "密码错误");
            }
        } else {
            respond.put("code", 403);
            respond.put("message", "没有此用户");
        }
        return respond.toJSONString();
    }

    @PostMapping("/register")
    public String addUser(@RequestBody User user) {
        user.setToken(user.getUsername() + "-token");
        Date date = new Date();
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMddHHmmss");
        user.setNickname("用户" + simpleDateFormat.format(date));
        JSONObject respond = new JSONObject();
        if (userService.selectByUsername(user.getUsername()) != null) {
            respond.put("code", 401);
            respond.put("message", "用户名已存在");
            return respond.toJSONString();
        }
        if (userService.addUser(user) == 1) {
            respond.put("code", 200);
            respond.put("message", "注册成功");
        } else {
            respond.put("code", 403);
            respond.put("message", "注册失败");
        }
        return respond.toJSONString();
    }

    @PostMapping("/userInfo")
    public String userInfo(@RequestBody Map<String, String> map) {
        JSONObject respond = new JSONObject();
        String token = map.get("token");
        User user = userService.selectByToken(token);
        if (user != null) {
            respond.put("code", 200);
            respond.put("message", "查询用户信息成功");
            respond.put("data", user);
        } else {
            respond.put("code", 404);
            respond.put("message", "查询用户信息失败");
        }
        return respond.toJSONString();
    }

    @GetMapping("/getUserInfoById")
    public String getUserInfoById (int id) {
        JSONObject res = new JSONObject();
        User user = userService.selectById(id);
        if (user != null) {
            user.setToken("#");
            user.setUsername("#");
            user.setPassword("#");
            res.put("code", 200);
            res.put("message", "根据id获取用户信息成功");
            res.put("data", user);
        } else {
            res.put("code", 403);
            res.put("message", "根据id获取用户信息失败");
        }
        return res.toJSONString();
    }

    @PostMapping("/saveUserInfo")
    public String updateUserInfo(@RequestBody User user) {
        JSONObject respond = new JSONObject();
        if (userService.updateUserInfo(user) == 1) {
            respond.put("code", 200);
            respond.put("message", "保存修改");
        } else {
            respond.put("code", 403);
            respond.put("message", "保存失败");
        }
        return respond.toJSONString();
    }

    @PostMapping("/updateCoin")
    public String updateCoin(@RequestBody Map<String, String> map) {
        JSONObject respond = new JSONObject();
        int num = Integer.parseInt(map.get("num"));
        int id = Integer.parseInt(map.get("id"));
        String token = map.get("token");
        if (userService.updateCoin(num, id, token) == 1) {
            respond.put("code", 200);
            respond.put("message", "修改硬币成功");
        } else {
            respond.put("code", 403);
            respond.put("message", "修改硬币失败");
        }
        return respond.toJSONString();
    }
}
